package com.jp.knowledge.util;

import android.content.Context;

import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.jp.knowledge.model.VoiceInputModel;

/**
 * Created by 528787067@qq.com on 2017/3/27.
 * 本来想使用单例的，将语言输入对话框设置为全局的对话框，
 * 无奈坑爹的 miui 默认会拦截悬浮窗权限，只能给为每次都初始化一次了
 */

public class VoiceInputUtils {
    private /*static*/ RecognizerDialog recognizerDialog;
//    private static VoiceInputUtils voiceInputUtils;
    private VoiceInputUtils(Context context){
        recognizerDialog = new RecognizerDialog(context/*.getApplicationContext()*/, new InitListener() {
            @Override
            public void onInit(int i) {
                L.i("讯飞语言初始化完成");
            }
        });
//        recognizerDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
        recognizerDialog.setParameter(SpeechConstant.DOMAIN, "iat");
        recognizerDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        recognizerDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
        recognizerDialog.setListener(new RecognizerDialogListener() {
            String result = "";
            @Override
            public void onResult(RecognizerResult recognizerResult, boolean b) {
                if(voiceInputCallback == null)
                    return;
                String currentInput = "";
                VoiceInputModel voiceModel = JsonUtil.getInstance()
                        .jsonToObject(recognizerResult.getResultString(), VoiceInputModel.class);
                for (VoiceInputModel.WsBean wsBean : voiceModel.getWs()){
                    for(VoiceInputModel.WsBean.CwBean cwBean : wsBean.getCw()){
                        currentInput += cwBean.getW();
                    }
                }
                currentInput = currentInput.trim()
                        .replaceAll("，", "")
                        .replaceAll("。", "")
                        .replaceAll("！", "")
                        .replaceAll("？", "")
                        .replaceAll("、", "")
                        .replaceAll("“", "")
                        .replaceAll("”", "")
                        .replaceAll(" ", "");
                result += currentInput;

                if(currentInput.length() > 0)
                    voiceInputCallback.onChanged(currentInput);
                if(b) {
                    voiceInputCallback.onResult(result);
                    result = "";
                }
            }
            @Override
            public void onError(SpeechError speechError) {
                result = "";
                if(voiceInputCallback != null)
                    voiceInputCallback.onError(speechError);
            }
        });
    }
    public static synchronized VoiceInputUtils getInstance(Context context){
//        if(voiceInputUtils == null)
//            voiceInputUtils = new VoiceInputUtils(context);
        return new VoiceInputUtils(context);
    }
    public void startInput(VoiceInputCallback voiceInputCallback){
        this.voiceInputCallback = voiceInputCallback;
        recognizerDialog.show();
    }

    private VoiceInputCallback voiceInputCallback;
    public interface VoiceInputCallback{
        void onChanged(String str);
        void onResult(String result);
        void onError(SpeechError speechError);
    }
}
